package com.immediatus.engine.input.sensor.accelerometer;

import android.hardware.SensorManager;
import android.view.Surface;
import com.immediatus.engine.input.sensor.BaseSensorData;

public class AccelerometerData extends BaseSensorData{
    private static final IAxisSwap AXISSWAPS[] = new IAxisSwap[4];

    static{
        AXISSWAPS[Surface.ROTATION_0] = new IAxisSwap(){
            @Override
            public void swapAxis(final float[] pValues){
                final float x = -pValues[SensorManager.DATA_X];
                final float y = pValues[SensorManager.DATA_Y];
                pValues[SensorManager.DATA_X] = x;
                pValues[SensorManager.DATA_Y] = y;
            }
        };

        AXISSWAPS[Surface.ROTATION_90] = new IAxisSwap(){
            @Override
            public void swapAxis(final float[] pValues){
                final float x = pValues[SensorManager.DATA_Y];
                final float y = pValues[SensorManager.DATA_X];
                pValues[SensorManager.DATA_X] = x;
                pValues[SensorManager.DATA_Y] = y;
            }
        };

        AXISSWAPS[Surface.ROTATION_180] = new IAxisSwap(){
            @Override
            public void swapAxis(final float[] pValues){
                final float x = pValues[SensorManager.DATA_X];
                final float y = -pValues[SensorManager.DATA_Y];
                pValues[SensorManager.DATA_X] = x;
                pValues[SensorManager.DATA_Y] = y;
            }
        };

        AXISSWAPS[Surface.ROTATION_270] = new IAxisSwap(){
            @Override
            public void swapAxis(final float[] pValues){
                final float x = -pValues[SensorManager.DATA_Y];
                final float y = -pValues[SensorManager.DATA_X];
                pValues[SensorManager.DATA_X] = x;
                pValues[SensorManager.DATA_Y] = y;
            }
        };
    }

    public AccelerometerData(final int orientation_){
        super(3, orientation_);
    }

    public float getX(){
        return this._Values[SensorManager.DATA_X];
    }

    public float getY(){
        return this._Values[SensorManager.DATA_Y];
    }

    public float getZ(){
        return this._Values[SensorManager.DATA_Z];
    }

    public void setX(final float x_){
        this._Values[SensorManager.DATA_X] = x_;
    }

    public void setY(final float y_){
        this._Values[SensorManager.DATA_Y] = y_;
    }

    public void setZ(final float z_){
        this._Values[SensorManager.DATA_Z] = z_;
    }

    @Override
    public void setValues(final float[] values_){
        super.setValues(values_);

        AccelerometerData.AXISSWAPS[this._DisplayRotation].swapAxis(this._Values);
    }

    private static interface IAxisSwap{
        public void swapAxis(final float[] pValues);
    }
}
